package com.ys.web.mobao.job;

import com.alibaba.fastjson.JSONObject;
import com.ys.utils.base.j2se.Logger;
import com.ys.utils.mobao.MobaoPay;
import com.ys.utils.redis.Redis;
import com.ys.utils.redis.key.UserKey;
import com.ys.utils.pay.Result;
import com.ys.web.mobao.service.MobaoWithholdingService;
import com.ys.web.pay.PayEnum;
import com.ys.web.pay.manager.PayManager;
import com.ys.web.pay.model.WithdrawRecord;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 代付订单状态查询
 * 每1秒跑一次，找到2秒前的提现订单，去支付公司查询该笔订单状态，更新订单信息
 */
@Component
public class MobaoWithholdingStatus implements Job {
    @Autowired
    private MobaoWithholdingService mobaoWithholdingService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<WithdrawRecord> list = mobaoWithholdingService.getWithdrawRecordListByTime();
        list.forEach(withdrawRecord->{
            try{
                JSONObject jsonObject = MobaoPay.fundoutQuery(withdrawRecord.getOrderNo());
                String status = jsonObject.getString("orderStatus");
                //失败处理
                switch (status) {
                    case "02":
                    case "08":
                    case "-1": {
                        withdrawRecord.setWithdrawStatus(PayEnum.WithdrawStatus.WITHDRAWAL_FAIL.getCode());
                        withdrawRecord.setRemark(jsonObject.getString("retMsg"));
                        int i = mobaoWithholdingService.updateWithdrawRecordByOrderId(withdrawRecord);
                        if(i==1){
                            //提现失败退回余额
                            Result<?> result1 = null;
                            if(withdrawRecord.payChannel==3){
                                //提现失败退回余额
                                result1 = PayManager.accountModifyZfb(withdrawRecord.getUserId(), PayEnum.AccountFlowType.WITHDRAWAL_FAIL, withdrawRecord.getAmount(), "notify.getErrorMessage()");

                            }else{
                                result1 = PayManager.accountModify(withdrawRecord.getUserId(), PayEnum.AccountFlowType.WITHDRAWAL_FAIL, withdrawRecord.getAmount(), "notify.getErrorMessage()",withdrawRecord.payChannel);
                            }
                            //次数-1
                            if (!result1.success()) {
                                String key = UserKey.withholdingCreateKey(withdrawRecord.getDayKey(), withdrawRecord.getUserId());
                                Redis.key.increment(key, -1);
                                String freeKey = UserKey.withholdingFreeKey(withdrawRecord.getDayKey(), withdrawRecord.getUserId());
                                Redis.key.increment(freeKey, -1);
                            }
                        }
                        break;
                    }
//                            case "00": {
//                                //提现成功扣除冻结金额
//                                Result<?> result1 = PayManager.accountModify(withdrawRecord.getUserId(), PayEnum.AccountFlowType.WITHDRAWAL_SUCCESS, withdrawRecord.getAmount(), StringHandler.getString(withdrawRecord.getUserId()));
//                                //次数-1
//                                if (result1.success()) {
//                                    withdrawRecord.setWithdrawStatus(PayEnum.WithdrawStatus.WITHDRAWAL_SUCCESS.getCode());
//                                    int i = mobaoWithholdingService.updateWithdrawRecordByOrderId(withdrawRecord);
//                                    if (i != 1) {
//                                        SystemError.wrapBs("500", "修改提现成功状态失败");
//                                    }
//                                }
//                                break;
//                            }
                }
            }catch (Exception e){
                Logger.error("订单状态处理异常,订单号："+withdrawRecord.getOrderNo(),e);
            }
        });
    }
}
